package com.dlc.felear.xiaoensale.utils;

import android.text.TextUtils;
import android.util.Log;

import com.dlc.felear.xiaoensale.constant.Constant;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class JasyptUtil {
    private static String TAG = "JasyptUtil";
    private static JasyptUtil instance;
    private StandardPBEStringEncryptor stringEncryptor;
    private String key;

    public static JasyptUtil getInstance() {
        if (instance == null) {
            instance = new JasyptUtil();
        }
        return instance;
    }

    private JasyptUtil() {
        stringEncryptor = new StandardPBEStringEncryptor();
        stringEncryptor.isInitialized();
        stringEncryptor.setPassword(Constant.JASYPT_ENCRYPTOR_PASSWORD);
        key = decrypt(HttpApi.JASYPT_KEY);
    }

    public String encrypt(String info) {
        return stringEncryptor.encrypt(info);
    }

    public String decrypt(String info) {
        return stringEncryptor.decrypt(info);
    }

    public String getSign(Map<String, Object> map) {
        ArrayList<String> list = new ArrayList<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            Object temp = entry.getValue();
            String key = entry.getKey();
            if (!TextUtils.isEmpty(key)) {
                list.add(key + "=" + temp + "&");
            }
        }
        int size = list.size();
        String[] arrayToSort = list.toArray(new String[size]);
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(arrayToSort[i]);
        }
        String result = sb.toString();
        result += "key=" + key;
        Log.e(TAG, " result=" + result);
        result = MD5Util.MD5(result).toUpperCase();
        Log.e(TAG, " Md5Result=" + result);
        return result;
    }

    public String getSign(LinkedHashMap<String, Object> map) {
        ArrayList<String> list = new ArrayList<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            Object temp = entry.getValue();
            String key = entry.getKey();
            if (!TextUtils.isEmpty(key)) {
                list.add(key + "=" + temp + "&");
            }
        }
        int size = list.size();
        String[] arrayToSort = list.toArray(new String[size]);
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(arrayToSort[i]);
        }
        String result = sb.toString();
        result += "key=" + key;
        Log.e(TAG, " result=" + result);
        result = MD5Util.MD5(result).toUpperCase();
        Log.e(TAG, " Md5Result=" + result);
        return result;
    }

}
